<template>
  <div class="groupContainer">
    <el-row class="title">
      <el-col :span="24">
        <div class="orangeIcon" />
        <span class="spanStyle">可授权功能</span>
        <el-tooltip class="item" effect="dark" :content="`用于确定管理员可以给业务用户授予的功能权限`"
                    placement="top-start">
          <i class="el-icon-question" />
        </el-tooltip>
        <div class="rangeDiv">
          <span>{{ range }}</span>
        </div>
      </el-col>
    </el-row>
    <el-row v-if="!isEdit" class="main">
      <template v-if="selectData.length > 0 && range ==='指定功能'">
        <el-col v-for="item in selectData" :key="item.orgId" :span="12">
          <el-tree :data="[item]" :props="defaultProps" />
        </el-col>
      </template>
      <div v-else-if="range ==='同上级管理员组'">
        <span class="nullSpan">同上级管理员组</span>
      </div>
      <div v-else>
        <span class="nullSpan">无</span>
      </div>
    </el-row>
    <div v-else class="main">
      <selectApply
        ref="selectApply"
        :column-props="columnProps"
        :default-props="defaultProps"
        :select-data="selectData"
        @getChecked="getChecked"
      />
    </div>
    <el-divider />
  </div>
</template>

<script>
import adminManageApi from '@/framework/api/authCenter/adminManage/adminManage'
import selectApply from '@/framework/components/addPerson/dt-select-apply.vue'

export default {
  name: 'AuthorizFeatures',
  components: { selectApply },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    groupId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      range: '',
      columnProps: {
        groupId: this.groupId,
        relType: 0
      },
      mayAuthorizeFunction: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      selectData: [],
      selectData1: []
    }
  },
  watch: {
    // mayAuthorizeFunction(newVal, oldVal) {
    //   this.$emit("input", newVal);
    // },
    isEdit(newVal, oldVal) {
      if (newVal) {
        this.$nextTick(() => {
          this.$refs.selectApply.handleInit()
        })
      }
    }
  },
  mounted() {
    this.getDatas()
  },
  methods: {
    getDatas() {
      adminManageApi
        .getCurrentGroupFunction({
          groupId: this.groupId,
          relType: 0
        })
        .then((resData) => {
          this.selectData = resData.data.appMenuButtonList
          this.range = resData.data.sameAsSuperior ? '同上级管理员组' : '指定功能'
          // const orgs = this.selectData?.map((res) => res.id).join(",");
          this.selectData1 = []
          this.treeToArr(this.selectData)
          this.$emit('input', this.selectData1)
          this.$emit('getLoading', 1)
        })
    },
    getChecked(val) {
      this.mayAuthorizeFunction = val
      this.$emit('input', this.mayAuthorizeFunction)
    },
    treeToArr(data) {
      data.map((item) => {
        if (item.children && item.children.length) {
          this.treeToArr(item.children)
        }
        this.selectData1.push(item)
        return null
      })
    }
  }
}
</script>
<style scoped lang="scss">
.title {
  height: 24px;
  display: flex;
  align-items: center;
  margin-bottom: 24px;

  .spanStyle {
    font-size: 14px;
    font-weight: 400;
    color: #131414;
    line-height: 24px;
  }

  .orangeIcon {
    width: 3px;
    height: 12px;
    background: #ff8800;
    display: inline-block;
    margin-right: 6px;
    border-radius: 92px;
  }

  .item {
    width: 14px;
    height: 14px;
    color: var(--themeColor);
    margin-right: 16px;
  }

  .rangeDiv {
    height: 24px;
    background: #fcf6ec;
    border-radius: 4px;
    opacity: 1;
    border: 1px solid #f2d09d;
    display: inline-block;
    padding: 0 10px;
    line-height: 20px;

    span {
      height: 20px;
      font-size: 12px;
      font-family: HarmonyOS Sans SC-Regular, HarmonyOS Sans SC;
      font-weight: 400;
      color: #e6a23c;
    }
  }
}

.main {
  display: flex;
  // align-items: center;
  margin-bottom: 25px;
  flex-direction: column;

  .nullSpan {
    font-size: 14px;
    font-weight: 400;
    color: #606266;
    line-height: 24px;
  }
}

::v-deep .el-tree-node__content {
  height: 38px;
  font-weight: 400;
}

::v-deep .el-tree-node__label {
  line-height: 24px;
}
</style>
